<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>商品修改</title>
		<script type="text/javascript">
			var obj=new URLSearchParams(location.search);
			var _pid=obj.get("pid");
			function sea(){
				//var _pid=pid.value;
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						if(r==0){
							alert("没有这个商品");
						}else{
							var arr=JSON.parse(r);
							pid.value=arr[0].pid;
							if(arr[0].category_id==1){
								one.checked=true;
							}else if(arr[0].category_id==0){
								two.checked=true;
							}else{
								three.checked=true;
							}
							name.value=arr[0].name;
							tastes.value=arr[0].tastes;
							title.value=arr[0].title;
							price.value=arr[0].price;
							spec.value=arr[0].spec;
							weigth.value=arr[0].weigth;
							sweetness.value=arr[0].sweetness;
							details.value=arr[0].details;
							shelf_time.value=arr[0].shelf_time;
							sold_count.value=arr[0].sold_count;
							if(arr[0].is_onsale==1){
								yes.checked=true;
							}else{
								no.checked=true;
							}
						}
					}
				}
				xhr.open("get","/product/product_search/"+_pid,true);
				xhr.send();
			}
			
			function update(){
				var obj=new URLSearchParams(location.search);
				//var _pid=pid.value;
				var _category_id="1";
				if(one.checked){
					_category_id=one.value;
				}else if(two.checked){
					_category_id=two.value;
				}else{
					_category_id=three.value;
				}
				var _name=name.value;
				var _tastes=tastes.value;
				var _title=title.value;
				var _price=price.value;
				var _spec=spec.value;
				var _weigth=weigth.value;
				var _sweetness=sweetness.value;
				var _details=details.value;
				var _shelf_time=shelf_time.value;
				var _sold_count=sold_count.value;
				var _is_onsale="-1";
				if(yes.checked){
					_is_onsale=yes.value;
				}else{
					_is_onsale=no.value;
				}
				var xhr=new XMLHttpRequest();
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						if(r==1){
							alert("修改成功");
							location.href="product_list.html";
						}else{
							alert("修改失败");
						}
					}
				}
				xhr.open("put","/product/product_update",true);
				var formdata=`pid=${_pid}&category_id=${_category_id}&name=${_name}&tastes=${_tastes}&title=${_title}&price=${_price}&spec=${_spec}&weigth=${_weigth}&sweetness=${_sweetness}&details=${_details}&is_onsale=${_is_onsale}`;
				xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				xhr.send(formdata);
			}
		</script>
	</head>
	<body onload="sea()">
		<p align="center">
		商品编号<input type="text" id="pid" readonly><br>
		所属商品分类<input type="radio" name="category_id" value="1" id="one">蛋糕
		<input type="radio" name="category_id" value="0" id="two">零食<br>
		商品名称<input type="text" id="name"><br>
		所属食品分类标号<input type="text" id="tastes"><br>
		主标题<input type="text" id="title"><br>
		价格<input type="text" id="price"><br>
		尺寸<input type="text" id="spec"><br>
		重量<input type="text" id="weigth"><br>
		甜度<input type="text" id="sweetness"><br>
    <!--input type="text" id="max_meals_num" readonly><br>
	  <input type="text" id="pid" readonly><br-->
		商品详细说明<textarea rows="5" cols="50" id="details"></textarea><br>
		上架时间<input type="text" id="shelf_time"><br>
		已售出数量<input type="text" id="sold_count"><br>
		是否在售<input type="radio" name="is_onsale" value="1" id="yes">是
		<input type="radio" name="is_onsale" value="0" id="no">否<br>
		<!--button onclick="sea()">查询</button-->
		<button onclick="update()">修改</button>
		</p>
	</body>
</html>
